﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

using ATA.Entities;
using ATA.Data;

#endregion

namespace ATA.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="ChuongTrinhChiTietProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract partial class ChuongTrinhChiTietProviderBaseCore : EntityProviderBase<ATA.Entities.ChuongTrinhChiTiet, ATA.Entities.ChuongTrinhChiTietKey>
	{		
		#region Get from Many To Many Relationship Functions
		#endregion	
		
		#region Delete Methods

		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to delete.</param>
		/// <returns>Returns true if operation suceeded.</returns>
		public override bool Delete(TransactionManager transactionManager, ATA.Entities.ChuongTrinhChiTietKey key)
		{
			return Delete(transactionManager, key.ChiTietId);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="chiTietId">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public bool Delete(System.Int32 chiTietId)
		{
			return Delete(null, chiTietId);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="chiTietId">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public abstract bool Delete(TransactionManager transactionManager, System.Int32 chiTietId);		
		
		#endregion Delete Methods
		
		#region Get By Foreign Key Functions
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChuongTrinhChiTiet_ChuongTrinh key.
		///		FK_ChuongTrinhChiTiet_ChuongTrinh Description: 
		/// </summary>
		/// <param name="chuongTrinhId"></param>
		/// <returns>Returns a typed collection of ATA.Entities.ChuongTrinhChiTiet objects.</returns>
		public ATA.Entities.TList<ChuongTrinhChiTiet> GetByChuongTrinhId(System.Int32 chuongTrinhId)
		{
			int count = -1;
			return GetByChuongTrinhId(chuongTrinhId, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChuongTrinhChiTiet_ChuongTrinh key.
		///		FK_ChuongTrinhChiTiet_ChuongTrinh Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="chuongTrinhId"></param>
		/// <returns>Returns a typed collection of ATA.Entities.ChuongTrinhChiTiet objects.</returns>
		/// <remarks></remarks>
		public ATA.Entities.TList<ChuongTrinhChiTiet> GetByChuongTrinhId(TransactionManager transactionManager, System.Int32 chuongTrinhId)
		{
			int count = -1;
			return GetByChuongTrinhId(transactionManager, chuongTrinhId, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChuongTrinhChiTiet_ChuongTrinh key.
		///		FK_ChuongTrinhChiTiet_ChuongTrinh Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="chuongTrinhId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.ChuongTrinhChiTiet objects.</returns>
		public ATA.Entities.TList<ChuongTrinhChiTiet> GetByChuongTrinhId(TransactionManager transactionManager, System.Int32 chuongTrinhId, int start, int pageLength)
		{
			int count = -1;
			return GetByChuongTrinhId(transactionManager, chuongTrinhId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChuongTrinhChiTiet_ChuongTrinh key.
		///		fkChuongTrinhChiTietChuongTrinh Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="chuongTrinhId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.ChuongTrinhChiTiet objects.</returns>
		public ATA.Entities.TList<ChuongTrinhChiTiet> GetByChuongTrinhId(System.Int32 chuongTrinhId, int start, int pageLength)
		{
			int count =  -1;
			return GetByChuongTrinhId(null, chuongTrinhId, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChuongTrinhChiTiet_ChuongTrinh key.
		///		fkChuongTrinhChiTietChuongTrinh Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="chuongTrinhId"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.ChuongTrinhChiTiet objects.</returns>
		public ATA.Entities.TList<ChuongTrinhChiTiet> GetByChuongTrinhId(System.Int32 chuongTrinhId, int start, int pageLength,out int count)
		{
			return GetByChuongTrinhId(null, chuongTrinhId, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChuongTrinhChiTiet_ChuongTrinh key.
		///		FK_ChuongTrinhChiTiet_ChuongTrinh Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="chuongTrinhId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of ATA.Entities.ChuongTrinhChiTiet objects.</returns>
		public abstract ATA.Entities.TList<ChuongTrinhChiTiet> GetByChuongTrinhId(TransactionManager transactionManager, System.Int32 chuongTrinhId, int start, int pageLength, out int count);
		
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChuongTrinhChiTiet_ChuongTrinhChiTiet key.
		///		FK_ChuongTrinhChiTiet_ChuongTrinhChiTiet Description: 
		/// </summary>
		/// <param name="loaiThoiGianId">Thang; Quy; Nam</param>
		/// <returns>Returns a typed collection of ATA.Entities.ChuongTrinhChiTiet objects.</returns>
		public ATA.Entities.TList<ChuongTrinhChiTiet> GetByLoaiThoiGianId(System.Byte loaiThoiGianId)
		{
			int count = -1;
			return GetByLoaiThoiGianId(loaiThoiGianId, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChuongTrinhChiTiet_ChuongTrinhChiTiet key.
		///		FK_ChuongTrinhChiTiet_ChuongTrinhChiTiet Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="loaiThoiGianId">Thang; Quy; Nam</param>
		/// <returns>Returns a typed collection of ATA.Entities.ChuongTrinhChiTiet objects.</returns>
		/// <remarks></remarks>
		public ATA.Entities.TList<ChuongTrinhChiTiet> GetByLoaiThoiGianId(TransactionManager transactionManager, System.Byte loaiThoiGianId)
		{
			int count = -1;
			return GetByLoaiThoiGianId(transactionManager, loaiThoiGianId, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChuongTrinhChiTiet_ChuongTrinhChiTiet key.
		///		FK_ChuongTrinhChiTiet_ChuongTrinhChiTiet Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="loaiThoiGianId">Thang; Quy; Nam</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.ChuongTrinhChiTiet objects.</returns>
		public ATA.Entities.TList<ChuongTrinhChiTiet> GetByLoaiThoiGianId(TransactionManager transactionManager, System.Byte loaiThoiGianId, int start, int pageLength)
		{
			int count = -1;
			return GetByLoaiThoiGianId(transactionManager, loaiThoiGianId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChuongTrinhChiTiet_ChuongTrinhChiTiet key.
		///		fkChuongTrinhChiTietChuongTrinhChiTiet Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="loaiThoiGianId">Thang; Quy; Nam</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.ChuongTrinhChiTiet objects.</returns>
		public ATA.Entities.TList<ChuongTrinhChiTiet> GetByLoaiThoiGianId(System.Byte loaiThoiGianId, int start, int pageLength)
		{
			int count =  -1;
			return GetByLoaiThoiGianId(null, loaiThoiGianId, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChuongTrinhChiTiet_ChuongTrinhChiTiet key.
		///		fkChuongTrinhChiTietChuongTrinhChiTiet Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="loaiThoiGianId">Thang; Quy; Nam</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.ChuongTrinhChiTiet objects.</returns>
		public ATA.Entities.TList<ChuongTrinhChiTiet> GetByLoaiThoiGianId(System.Byte loaiThoiGianId, int start, int pageLength,out int count)
		{
			return GetByLoaiThoiGianId(null, loaiThoiGianId, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChuongTrinhChiTiet_ChuongTrinhChiTiet key.
		///		FK_ChuongTrinhChiTiet_ChuongTrinhChiTiet Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="loaiThoiGianId">Thang; Quy; Nam</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of ATA.Entities.ChuongTrinhChiTiet objects.</returns>
		public abstract ATA.Entities.TList<ChuongTrinhChiTiet> GetByLoaiThoiGianId(TransactionManager transactionManager, System.Byte loaiThoiGianId, int start, int pageLength, out int count);
		
		#endregion

		#region Get By Index Functions
		
		/// <summary>
		/// 	Gets a row from the DataSource based on its primary key.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to retrieve.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <returns>Returns an instance of the Entity class.</returns>
		public override ATA.Entities.ChuongTrinhChiTiet Get(TransactionManager transactionManager, ATA.Entities.ChuongTrinhChiTietKey key, int start, int pageLength)
		{
			return GetByChiTietId(transactionManager, key.ChiTietId, start, pageLength);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key PK_ChuongTrinhChiTiet index.
		/// </summary>
		/// <param name="chiTietId"></param>
		/// <returns>Returns an instance of the <see cref="ATA.Entities.ChuongTrinhChiTiet"/> class.</returns>
		public ATA.Entities.ChuongTrinhChiTiet GetByChiTietId(System.Int32 chiTietId)
		{
			int count = -1;
			return GetByChiTietId(null,chiTietId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ChuongTrinhChiTiet index.
		/// </summary>
		/// <param name="chiTietId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="ATA.Entities.ChuongTrinhChiTiet"/> class.</returns>
		public ATA.Entities.ChuongTrinhChiTiet GetByChiTietId(System.Int32 chiTietId, int start, int pageLength)
		{
			int count = -1;
			return GetByChiTietId(null, chiTietId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ChuongTrinhChiTiet index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="chiTietId"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="ATA.Entities.ChuongTrinhChiTiet"/> class.</returns>
		public ATA.Entities.ChuongTrinhChiTiet GetByChiTietId(TransactionManager transactionManager, System.Int32 chiTietId)
		{
			int count = -1;
			return GetByChiTietId(transactionManager, chiTietId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ChuongTrinhChiTiet index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="chiTietId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="ATA.Entities.ChuongTrinhChiTiet"/> class.</returns>
		public ATA.Entities.ChuongTrinhChiTiet GetByChiTietId(TransactionManager transactionManager, System.Int32 chiTietId, int start, int pageLength)
		{
			int count = -1;
			return GetByChiTietId(transactionManager, chiTietId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ChuongTrinhChiTiet index.
		/// </summary>
		/// <param name="chiTietId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="ATA.Entities.ChuongTrinhChiTiet"/> class.</returns>
		public ATA.Entities.ChuongTrinhChiTiet GetByChiTietId(System.Int32 chiTietId, int start, int pageLength, out int count)
		{
			return GetByChiTietId(null, chiTietId, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ChuongTrinhChiTiet index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="chiTietId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="ATA.Entities.ChuongTrinhChiTiet"/> class.</returns>
		public abstract ATA.Entities.ChuongTrinhChiTiet GetByChiTietId(TransactionManager transactionManager, System.Int32 chiTietId, int start, int pageLength, out int count);
						
		#endregion "Get By Index Functions"
	
		#region Custom Methods
		
		
		#endregion

		#region Helper Functions	
		
		/// <summary>
		/// Fill a ATA.Entities.TList&lt;ChuongTrinhChiTiet&gt; From a DataReader.
		/// </summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">number of rows.</param>
		/// <returns>a <see cref="ATA.Entities.TList&lt;ChuongTrinhChiTiet&gt;"/></returns>
		public static ATA.Entities.TList<ChuongTrinhChiTiet> Fill(IDataReader reader, ATA.Entities.TList<ChuongTrinhChiTiet> rows, int start, int pageLength)
		{
		// advance to the starting row
		for (int i = 0; i < start; i++)
		{
			if (!reader.Read())
			return rows; // not enough rows, just return
		}
		for (int i = 0; i < pageLength; i++)
		{
			if (!reader.Read())
			break; // we are done
			string key = null;
			
			ATA.Entities.ChuongTrinhChiTiet c = null;
			if (DataRepository.Provider.UseEntityFactory)
			{
			key = new System.Text.StringBuilder("ChuongTrinhChiTiet")
			.Append("|").Append((reader.IsDBNull(((int)ATA.Entities.ChuongTrinhChiTietColumn.ChiTietId - 1))?(int)0:(System.Int32)reader[((int)ATA.Entities.ChuongTrinhChiTietColumn.ChiTietId - 1)]).ToString()).ToString();
			c = EntityManager.LocateOrCreate<ChuongTrinhChiTiet>(
			key.ToString(), // EntityTrackingKey
			"ChuongTrinhChiTiet",  //Creational Type
			DataRepository.Provider.EntityCreationalFactoryType,  //Factory used to create entity
			DataRepository.Provider.EnableEntityTracking); // Track this entity?
			}
			else
			{
			c = new ATA.Entities.ChuongTrinhChiTiet();
			}
			
			if (!DataRepository.Provider.EnableEntityTracking ||
			c.EntityState == EntityState.Added ||
			(DataRepository.Provider.EnableEntityTracking &&
			((DataRepository.Provider.CurrentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
			(DataRepository.Provider.CurrentLoadPolicy == LoadPolicy.DiscardChanges && (c.EntityState == EntityState.Unchanged ||
								c.EntityState == EntityState.Changed)))))
						{
			c.SuppressEntityEvents = true;
			c.ChiTietId = (System.Int32)reader["ChiTietID"];
			c.LoaiThoiGianId = (System.Byte)reader["LoaiThoiGianID"];
			c.SoThoiGian = (System.Byte)reader["SoThoiGian"];
			c.DoanhSo = (System.Int32)reader["DoanhSo"];
			c.ChuongTrinhId = (System.Int32)reader["ChuongTrinhID"];
			c.LastUpdate = (System.DateTime)reader["LastUpdate"];
			c.UpdateBy = (System.Int32)reader["UpdateBy"];
			c.EntityTrackingKey = key;
			c.AcceptChanges();
			c.SuppressEntityEvents = false;
			}
			rows.Add(c);
		}
		return rows;
		}		
		/// <summary>
		/// Refreshes the <see cref="ATA.Entities.ChuongTrinhChiTiet"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="ATA.Entities.ChuongTrinhChiTiet"/> object to refresh.</param>
		public static void RefreshEntity(IDataReader reader, ATA.Entities.ChuongTrinhChiTiet entity)
		{
			if (!reader.Read()) return;
			
			entity.ChiTietId = (System.Int32)reader["ChiTietID"];
			entity.LoaiThoiGianId = (System.Byte)reader["LoaiThoiGianID"];
			entity.SoThoiGian = (System.Byte)reader["SoThoiGian"];
			entity.DoanhSo = (System.Int32)reader["DoanhSo"];
			entity.ChuongTrinhId = (System.Int32)reader["ChuongTrinhID"];
			entity.LastUpdate = (System.DateTime)reader["LastUpdate"];
			entity.UpdateBy = (System.Int32)reader["UpdateBy"];
			entity.AcceptChanges();
		}
		
		/// <summary>
		/// Refreshes the <see cref="ATA.Entities.ChuongTrinhChiTiet"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="ATA.Entities.ChuongTrinhChiTiet"/> object.</param>
		public static void RefreshEntity(DataSet dataSet, ATA.Entities.ChuongTrinhChiTiet entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.ChiTietId = (System.Int32)dataRow["ChiTietID"];
			entity.LoaiThoiGianId = (System.Byte)dataRow["LoaiThoiGianID"];
			entity.SoThoiGian = (System.Byte)dataRow["SoThoiGian"];
			entity.DoanhSo = (System.Int32)dataRow["DoanhSo"];
			entity.ChuongTrinhId = (System.Int32)dataRow["ChuongTrinhID"];
			entity.LastUpdate = (System.DateTime)dataRow["LastUpdate"];
			entity.UpdateBy = (System.Int32)dataRow["UpdateBy"];
			entity.AcceptChanges();
		}
		#endregion 
		
		#region DeepLoad Methods
		/// <summary>
		/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
		/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
		/// </summary>
		/// <remarks>
		/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
		/// </remarks>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="entity">The <see cref="ATA.Entities.ChuongTrinhChiTiet"/> object to load.</param>
		/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
		/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
		/// <param name="childTypes">ATA.Entities.ChuongTrinhChiTiet Property Collection Type Array To Include or Exclude from Load</param>
		/// <param name="innerList">A collection of child types for easy access.</param>
	    /// <exception cref="ArgumentNullException">entity or childTypes is null.</exception>
	    /// <exception cref="ArgumentException">deepLoadType has invalid value.</exception>
		internal override void DeepLoad(TransactionManager transactionManager, ATA.Entities.ChuongTrinhChiTiet entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes, DeepSession innerList)
		{
			if(entity == null)
				return;

			#region ChuongTrinhIdSource	
			if (CanDeepLoad(entity, "ChuongTrinh|ChuongTrinhIdSource", deepLoadType, innerList) 
				&& entity.ChuongTrinhIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.ChuongTrinhId;
				ChuongTrinh tmpEntity = EntityManager.LocateEntity<ChuongTrinh>(EntityLocator.ConstructKeyFromPkItems(typeof(ChuongTrinh), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.ChuongTrinhIdSource = tmpEntity;
				else
					entity.ChuongTrinhIdSource = DataRepository.ChuongTrinhProvider.GetByChuongTrinhId(transactionManager, entity.ChuongTrinhId);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'ChuongTrinhIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.ChuongTrinhIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.ChuongTrinhProvider.DeepLoad(transactionManager, entity.ChuongTrinhIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion ChuongTrinhIdSource

			#region LoaiThoiGianIdSource	
			if (CanDeepLoad(entity, "LoaiThoiGian|LoaiThoiGianIdSource", deepLoadType, innerList) 
				&& entity.LoaiThoiGianIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.LoaiThoiGianId;
				LoaiThoiGian tmpEntity = EntityManager.LocateEntity<LoaiThoiGian>(EntityLocator.ConstructKeyFromPkItems(typeof(LoaiThoiGian), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.LoaiThoiGianIdSource = tmpEntity;
				else
					entity.LoaiThoiGianIdSource = DataRepository.LoaiThoiGianProvider.GetByLoaiThoiGianId(transactionManager, entity.LoaiThoiGianId);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'LoaiThoiGianIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.LoaiThoiGianIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.LoaiThoiGianProvider.DeepLoad(transactionManager, entity.LoaiThoiGianIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion LoaiThoiGianIdSource
			
			//used to hold DeepLoad method delegates and fire after all the local children have been loaded.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			
			//Fire all DeepLoad Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			deepHandles = null;
		}
		
		#endregion 
		
		#region DeepSave Methods

		/// <summary>
		/// Deep Save the entire object graph of the ATA.Entities.ChuongTrinhChiTiet object with criteria based of the child 
		/// Type property array and DeepSaveType.
		/// </summary>
		/// <param name="transactionManager">The transaction manager.</param>
		/// <param name="entity">ATA.Entities.ChuongTrinhChiTiet instance</param>
		/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
		/// <param name="childTypes">ATA.Entities.ChuongTrinhChiTiet Property Collection Type Array To Include or Exclude from Save</param>
		/// <param name="innerList">A Hashtable of child types for easy access.</param>
		internal override bool DeepSave(TransactionManager transactionManager, ATA.Entities.ChuongTrinhChiTiet entity, DeepSaveType deepSaveType, System.Type[] childTypes, DeepSession innerList)
		{	
			if (entity == null)
				return false;
							
			#region Composite Parent Properties
			//Save Source Composite Properties, however, don't call deep save on them.  
			//So they only get saved a single level deep.
			
			#region ChuongTrinhIdSource
			if (CanDeepSave(entity, "ChuongTrinh|ChuongTrinhIdSource", deepSaveType, innerList) 
				&& entity.ChuongTrinhIdSource != null)
			{
				DataRepository.ChuongTrinhProvider.Save(transactionManager, entity.ChuongTrinhIdSource);
				entity.ChuongTrinhId = entity.ChuongTrinhIdSource.ChuongTrinhId;
			}
			#endregion 
			
			#region LoaiThoiGianIdSource
			if (CanDeepSave(entity, "LoaiThoiGian|LoaiThoiGianIdSource", deepSaveType, innerList) 
				&& entity.LoaiThoiGianIdSource != null)
			{
				DataRepository.LoaiThoiGianProvider.Save(transactionManager, entity.LoaiThoiGianIdSource);
				entity.LoaiThoiGianId = entity.LoaiThoiGianIdSource.LoaiThoiGianId;
			}
			#endregion 
			#endregion Composite Parent Properties

			// Save Root Entity through Provider
			if (!entity.IsDeleted)
				this.Save(transactionManager, entity);
			
			//used to hold DeepSave method delegates and fire after all the local children have been saved.
			Dictionary<Delegate, object> deepHandles = new Dictionary<Delegate, object>();
			//Fire all DeepSave Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			
			// Save Root Entity through Provider, if not already saved in delete mode
			if (entity.IsDeleted)
				this.Save(transactionManager, entity);
				

			deepHandles = null;
						
			return true;
		}
		#endregion
	} // end class
	
	#region ChuongTrinhChiTietChildEntityTypes
	
	///<summary>
	/// Enumeration used to expose the different child entity types 
	/// for child properties in <c>ATA.Entities.ChuongTrinhChiTiet</c>
	///</summary>
	public enum ChuongTrinhChiTietChildEntityTypes
	{
		
		///<summary>
		/// Composite Property for <c>ChuongTrinh</c> at ChuongTrinhIdSource
		///</summary>
		[ChildEntityType(typeof(ChuongTrinh))]
		ChuongTrinh,
			
		///<summary>
		/// Composite Property for <c>LoaiThoiGian</c> at LoaiThoiGianIdSource
		///</summary>
		[ChildEntityType(typeof(LoaiThoiGian))]
		LoaiThoiGian,
		}
	
	#endregion ChuongTrinhChiTietChildEntityTypes
	
	#region ChuongTrinhChiTietFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;ChuongTrinhChiTietColumn&gt;"/> class
	/// that is used exclusively with a <see cref="ChuongTrinhChiTiet"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class ChuongTrinhChiTietFilterBuilder : SqlFilterBuilder<ChuongTrinhChiTietColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the ChuongTrinhChiTietFilterBuilder class.
		/// </summary>
		public ChuongTrinhChiTietFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the ChuongTrinhChiTietFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public ChuongTrinhChiTietFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the ChuongTrinhChiTietFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public ChuongTrinhChiTietFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion ChuongTrinhChiTietFilterBuilder
	
	#region ChuongTrinhChiTietParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;ChuongTrinhChiTietColumn&gt;"/> class
	/// that is used exclusively with a <see cref="ChuongTrinhChiTiet"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class ChuongTrinhChiTietParameterBuilder : ParameterizedSqlFilterBuilder<ChuongTrinhChiTietColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the ChuongTrinhChiTietParameterBuilder class.
		/// </summary>
		public ChuongTrinhChiTietParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the ChuongTrinhChiTietParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public ChuongTrinhChiTietParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the ChuongTrinhChiTietParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public ChuongTrinhChiTietParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion ChuongTrinhChiTietParameterBuilder
} // end namespace
